#Install SPARQL package: http://www.eionet.europa.eu/software/sparql-client/
from string import Template
import sparql
import sys
import getopt

#	q = Template('SELECT ?obs ?value ?indicator ?dataset ?country ?year ?status \n FROM <$graph> WHERE{ ?obs rdf:type qb:Observation.\n\
#			  ?obs wi-onto:ref-indicator ?indicator. \n\
#			  FILTER(?indicator = wi-indicator:$indicator). \n\
#			  ?obs qb:dataSet ?dataset. \n\
#			  FILTER(?dataset = wi-dataset:$indicator-$dataset). \n\
#			  ?obs wi-onto:ref-area ?country. \n\
#			  FILTER(?country = wi-country:$country). \n\
#			  ?obs wi-onto:ref-year ?year. \n\
#			  FILTER(?year = $year). \n\
#			  ?obs sdmx-concept:obsStatus ?status. \n\
#			   \n\
#			  ?obs wi-onto:value ?value.  \n}') 	
#
 #     print "Country: "+country
 #       for indicator in indicators:
#	        for dataset in datasets:
#			for year in years:
#				query = q.substitute(graph=graph, indicator=indicator, dataset=dataset, year=year, country=country)
#				result = sparql.query(endpoint, query)
#				print "Indicator: " + indicator +", Dataset: " + dataset+ ", Year: "+str(year)
#				for row in result:
#					values = sparql.unpack_row(row)
#					print values[1]
#
#
def checkIndicators(endpoint, graph, country, indicators, datasets, years):
	q = Template('ASK FROM <$graph> WHERE{ ?obs rdf:type qb:Observation.\n\
			  ?obs wi-onto:ref-indicator ?indicator. \n\
			  FILTER(?indicator = wi-indicator:$indicator). \n\
			  ?obs qb:dataSet ?dataset. \n\
			  FILTER(?dataset = wi-dataset:$indicator-$dataset). \n\
			  ?obs wi-onto:ref-area ?country. \n\
			  FILTER(?country = wi-country:$country). \n\
			  ?obs wi-onto:ref-year ?year. \n\
			  FILTER(?year = $year). \n\
			  ?obs sdmx-concept:obsStatus ?status. \n\
			  ?obs wi-onto:value ?value.  \n}') 	
        print "Country: "+country
        for indicator in indicators:
	        for dataset in datasets:
			for year in years:
				query = q.substitute(graph=graph, indicator=indicator, dataset=dataset, year=year, country=country)
				result = sparql.query(endpoint, query)
				if(not(result.hasresult())):
					print "Indicator: " + indicator +", Dataset: " + dataset+ ", Year: "+str(year)
					
			

if __name__ == '__main__':
	endpoint = 'http://data.webfoundation.org/sparql'
	graph = 'http://data.webfoundation.org/webindex/'
	country = 'FRA'
	primaryIndicators = ['Q18', 'Q20', 'Q9ab', 'Q9cd', 'Q9e', 'Q9f', 'Q9g', 'Q9h', 'Q9i', 'Q9l', 'Q10', 'Q13', 'Q16', 'Q25', 'Q11a', 'Q11b', 'Q11c', 'Q11d', 'Q11e', 'Q11f', 'Q2a', 'Q3', 'Q5a', 'Q5b', 'Q5c', 'Q8a', 'Q8b', 'Q8c', 'Q9k', 'Q22', 'Q23a', 'Q23b', 'Q23c', 'Q23d', 'Q23e', 'Q23f', 'Q23g', 'Q23h', 'Q23i', 'Q23j', 'Q24', 'Q26', 'Q4', 'Q6', 'Q7', 'Q12', 'Q14', 'Q15', 'Q17', 'Q1', 'Q2b']
	primaryDatasets = ['Raw', 'Normalised']
        primaryYears = [2011]
	
	secondaryIndicators =['ITUA' ,  'ITUB' ,  'WBA' ,  'WEFA' ,  'WEFB' ,  'ITUC' ,  'ITUD' ,  'IEAA' ,  'ITUE' ,  'ITUF' ,  'ITUG' ,  'RSFA' ,  'WBB' ,  'UNA' ,  'WEFC' ,  'WEFD' ,  'UNB' ,  'WEFE' ,  'WEFF' ,  'WEFG' ,  'WEFH' ,  'FHA' ,  'FHB' ,  'ITUH' ,  'WIKIA' ,  'UNC' ,  'WEFI' ,  'WEFJ' ,  'WBC' ,  'WEFK' ,  'WEFL' ,  'WEFM' ,  'UND' ,  'WEFN']
        secondaryDatasets = [ 'Imputed', 'Raw', 'Normalised']
        secondaryYears = [2007, 2008, 2009, 2010, 2011]
	checkIndicators(endpoint, graph, country, primaryIndicators, primaryDatasets, primaryYears)
	checkIndicators(endpoint, graph, country, secondaryIndicators, secondaryDatasets, secondaryYears)

	



